package core

import (
	"context"
	"golang.org/x/net/ipv4"
	"nprobe/structs"
)

type Receiver interface {
	GetName() string
	GetRBuf() *structs.PPRingBuffer[*SpanRecord]
	StartReceiver() error
	StopReceiver() error
}

type BaseReceiver struct {
	Ctx      context.Context
	Name     string
	ID       int
	Task     *ProbeTask
	RBuf     *structs.PPRingBuffer[*SpanRecord]
	Salts    map[int][]byte
	DstIPSet map[string]bool

	Conn           *ipv4.RawConn
	ErrCnt         int
	CurrSpanRecord *SpanRecord
}

func (r *BaseReceiver) GetName() string {
	return r.Name
}

func (r *BaseReceiver) GetRBuf() *structs.PPRingBuffer[*SpanRecord] {
	return r.RBuf
}

func (r *BaseReceiver) StopReceiver() error {
	if r != nil {
		if r.Conn != nil {
			r.Conn.Close()
		}
	}

	return nil
}

func AllocRxBuf(msgLen int) []byte {
	plen := msgLen + 80
	if plen < 1500 {
		plen = 1500
	}
	return make([]byte, plen)
}
